let v = new Vue({
    el: '#app',
    data: {
        hot:[],      //热销商品
        payCart:[],     //购物车商品集合
        totalPrice:'',
        v_totalPrice:'￥ 0'
    },
    methods: {
        toGoodsDetail:function (id) {
             localStorage.setItem("aid",id);
        },
        initPayCart:function () {
            axios(`cart/initPayCart`)
                .then(rsp=>{
                    if (rsp.data.flag){
                        this.hot = rsp.data.data.hot;
                        this.payCart = rsp.data.data.cart;
                        this.totalPrice = rsp.data.data.totalPrice;
                        this.v_totalPrice = "￥ " + this.totalPrice;
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        },
        settle:function (goods) {
            axios.put(`cart/settle`,goods)
                .then(rsp=>{
                    if (rsp.data.flag){
                        layer.msg("支付成功");
                        this.initPayCart();
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        },
        settleAll:function () {
            axios.put(`cart/settleAll`,this.payCart)
                .then(rsp=>{
                    if (rsp.data.flag){
                        layer.msg("支付成功");
                        this.initPayCart();
                    }
                })
                .catch(error=>{
                    layer.msg(error.message);
                });
        },
        deleteGoods:function (cart) {
            layer.confirm('是否删除该商品', {
                btn: ['确认','取消'], //按钮
                yes:()=>{
                    axios.put(`cart/deleteGoods`, cart
                    ).then(response=>{
                        if (response.data.flag){
                            layer.msg("删除成功");
                            this.initPayCart();
                        }
                    }).catch(e=>{
                        layer.msg(e.message);
                    });
                }
            })
        }
    },
    created: function () {
        this.initPayCart();
    }
});